using System;
using System.Collections.Generic;
using System.ComponentModel;

using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CodePlex.SpaceBlock;
using System.Threading;
using CodePlex.SpaceBlock.FS;
using CodePlex.Resourceful.Amazon.S3;

namespace CodePlex.SpaceBlock.UI.Controls
{
    public partial class PropertiesForm : CodePlex.SpaceBlock.UI.Controls.BaseForm
    {

        private readonly ActionManager _actions;
        private Thread _worker;
        private bool _pending;
        private IFileSystem _fileSystem;
        private List<Path> _paths;
        private MetadataControl _metadataControl;
        private ACLControl _aclControl;

        public PropertiesForm(ActionManager actions)
        {
            _actions = actions;

            InitializeComponent();

            this.VisibleChanged += delegate
            {
                if (_pending)
                {
                    this.UpdateData();
                    _pending = false;
                }
            };
        }

        public void Initialize(IFileSystem fileSystem, List<Path> paths)
        {
            _fileSystem = fileSystem;
            _paths = paths;

            if (this.Visible)
            {
                this.UpdateData();
            }
            else
            {
                _pending = true;
            }
        }










        private void _okButton_Click(object sender, EventArgs e)
        {
            this.Visible = false;
        }

        private void PropertiesForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                e.Cancel = true;
                this.Visible = false;
            }
        }

        private void UpdateData()
        {
            if (_metadataControl == null)
            {
                _metadataControl = new MetadataControl();
                UIUtils.FillWith(_splitContainer.Panel1, _metadataControl);
            }
            if (_aclControl == null)
            {
                _aclControl = new ACLControl(_actions);

                
                UIUtils.FillWith(_splitContainer.Panel2, _aclControl);
            }


            if (_paths.Count == 1)
            {
               
                Path path = _paths[0];

                lock (typeof(PropertiesForm))
                {
                    if (_worker != null)
                    {
                        _worker.Abort();

                        _worker = null;
                    }
                }
                _worker = this.InBackground(delegate
                {
                    Metadata metadata = _fileSystem.GetMetadata(path);
                    ACL acl = _fileSystem.GetACL(path);

                    this.InDisplay(delegate
                    {
                        _aclControl.ACL = acl;
                        _metadataControl.Metadata = metadata;

                        _pathBox.Text = path.Level==0?_fileSystem.Caption:path.Name;

                        string hint = path.IsFolder ? "Folder" : "File";
                        _iconBox.Image = UIManager.Instance.GetSmallImage(hint);

                    });
                });

            }
            else
            {
               
                if (_iconBox.Image != null)
                {
                    _iconBox.Image = null;
                }

                _aclControl.ACL = null;
                _metadataControl.Metadata = null;

                _pathBox.Text = string.Format("({0} Selection)",_paths.Count==0?"No":"Multiple");

             
            }

        }
    }
}

